<template>
  <p-drawer
    placement="right"
    :closable="false"
    width="calc(100vw - 200px)"
    :visible="visible"
    :mask-closable="false"
    :destroy-on-close="true"
    @close="onClose"
  >
    <TaskTitle icon-name="Det" title-name="线索转商机" color="green" />
    <div class="list-container">
      <div class="progress-container">
        <TitleSecondary title="审批流程图" />
        <ProgressClueStatus />
      </div>
      <ProgressClue :data="nodeFlowChartList" />
      <TitleSecondary title="基本信息" />
      <FormClueList :form="form" :clue-state="false" />
      <ProductList :data-source="detail.leadsProductList" />
      <ContactList :data-source="detail.leadsContactList" />
      <SalesTeamList :data-source="detail.leadsSalesTeamList" />
      <VisitList :data-source="detail.visitList" />
      <AttachList :data-source="detail.attachmentList" />
      <ProgressLog :list="operationLogInformation" />
      <BottomLine />
    </div>

    <ButtonGroup
      class="footer"
      :options="footerButtons"
      @click="handleButtonClick"
    />
    <DialogTransferAdmin
      :visible.sync="visibleModal"
      :plan-id="planId"
      :selecteds="selectedInfos"
      number="线索编号"
      @fresh="getList"
    />
  </p-drawer>
</template>

<script>
import {
  getOperationLog,
  getProcessNode,
  getRecallClue,
  postClueCreate,
} from '@/api/clue.js';
import ProgressClueStatus from '@/components/task/ProgressClueStatus.vue';
import FormClueList from '@/components/task/FormClueList.vue';
import ProductList from '@/components/table/ProductList.vue';
import ContactList from '@/components/table/ContactList.vue';
import SalesTeamList from '@/components/table/SalesTeamList.vue';
import VisitList from '@/components/table/VisitList.vue';
import AttachList from '@/components/table/AttachList.vue';
import ProgressClue from '@/components/task/ProgressClue.vue';
import DialogTransferAdmin from '@/components/dialog/DialogTransferAdmin.vue';
import ProgressLog from '@/components/task/ProgressLog.vue';
import DrawerMixin from './mixin';
export default {
  components: {
    ProgressClueStatus,
    FormClueList,
    ProductList,
    ContactList,
    SalesTeamList,
    VisitList,
    AttachList,
    ProgressClue,
    DialogTransferAdmin,
    ProgressLog,
  },
  mixins: [DrawerMixin],
  data() {
    return {
      visibleModal: false,
      operationLogInformation: [], //操作日志请求返回的信息
      nodeFlowChartList: [],
    };
  },
  computed: {
    form() {
      return this.detail || {};
    },
    footerButtons() {
      if (
        this.detail.createUserId === this.$store.state.user.uid &&
        this.detail.currStatus === '2' &&
        this.detail.nodeCode === '1'
      ) {
        return [
          {
            key: 'recall',
            label: '撤回',
            authKey: 'clue-create-button-recall',
          },
          {
            key: 'close',
            label: '返回',
          },
        ];
      } else {
        return [
          {
            key: 'close',
            label: '返回',
          },
        ];
      }
    },
  },
  watch: {
    visible: {
      handler(val) {
        val && this.handleChangeIcon();
        val && this.getProNode();
      },
      immediate: true,
    },
  },
  created() {
    console.log(this.$store.state.user.uid);
  },
  methods: {
    // 审批流程图
    async getProNode() {
      const res = await getProcessNode(
        this.processId,
        'leads_transfer_biz_process_node'
      );
      this.nodeFlowChartList = res?.data?.data || [];
    },
    // 操作日志
    async handleChangeIcon() {
      const res = await getOperationLog('leads', this.detail.leadsNo);
      this.operationLogInformation = res.data.data;
    },
    onClose() {
      this.$emit('update:visible', false);
      this.$emit('fresh');
    },
    //撤回
    async recallCallback() {
      try {
        await this.$confirm('撤回线索转商机，是否确认？');
        const res = await getRecallClue(this.detail.id);
        if (res.data.success) {
          const res = await postClueCreate({
            currStatus: '4',
            bizModule: 'leads',
            id: this.detail.processId,
          });
        }
        this.$message.success('撤回成功');
        this.onClose();
      } catch (error) {
        // console.log(error);
      }
    },
    // 返回
    closeCallback() {
      this.onClose();
    },
    handleButtonClick(key) {
      const fn = `${key}Callback`;
      this[fn] && this[fn]();
    },
  },
};
</script>

<style lang="less" scoped>
.footer {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #e5e6e9;
}
.list-container {
  height: calc(100vh - 110px);
  overflow-y: scroll;
  position: relative;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  padding: 10px 24px;
}

/deep/ .poros-drawer-body {
  min-width: 1136px;
  height: 100%;
  padding: 0;
}
.progress-container {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
</style>
